package com.nyist.myticket.web;

import com.alibaba.fastjson.JSONObject;
import com.nyist.myticket.biz.entity.User;
import com.nyist.myticket.biz.service.UserService;
import com.nyist.myticket.core.common.R;
import com.nyist.myticket.core.redis.RedisFactory;
import com.nyist.myticket.core.utils.DateUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * *
 *
 * @author yangxiaogen
 * @date 2021/12/23 15:18
 */
@RestController
//@RequestMapping("/test")
@Api(tags = "测试使用")
@Slf4j
public class TestController {

    @Autowired
    private UserService userService;


    @GetMapping("/userInfoCount")
    public R getUserInfoCount() {
        String userInfoCount = RedisFactory.getValue("userInfoCount"+"_key");
        if (StringUtils.isNotBlank(userInfoCount)) {
            log.info("=====>  从缓存中取到了数据, 用户数量为: {}", userInfoCount);
            return R.ok().put("200", "成功!")
                    .put("time", DateUtils.getLocalTime())
                    .put("date", userInfoCount);
        }
        List<User> list = userService.list();

        userInfoCount = String.valueOf(list.size());
        log.info("=====>  查询用户信息数量被调用了...数据大小为...{}", userInfoCount);
        RedisFactory.setValueWithoutExpire("userInfoCount" + "_key", userInfoCount);

        return R.ok().put("200", "成功!")
                .put("time", DateUtils.getLocalTime())
                .put("date", userInfoCount);
    }

    @GetMapping("/userInfo")
    public R getUserInfo(){
        List<User> list = userService.list();

       log.info("查询用户信息被调用了...数据大小为...{}", list.size());
        return R.ok().put("200","成功!")
                .put("time", DateUtils.getLocalTime())
                .put("date",list);
    }

    @GetMapping("/httpserver/heartbeat")
    public R getHaikong(@RequestParam String sn, @RequestParam String timestamp) {
        log.info("被调用了....");
        log.info("被调用了...in..{}", JSONObject.toJSONString(sn));
        log.info("被调用了...timestamp..{}", JSONObject.toJSONString(timestamp));

        List<User> list = userService.list();
        return R.ok().put("200", "成功!")
                .put("time", DateUtils.getLocalTime())
                .put("date", list);
    }

    @GetMapping("/httpserver/heartbeat2")
    public R getHaikong2(@RequestParam String sn, @RequestParam String timestamp) {
        log.info("被调用了....");
        log.info("被调用了...in..{}", JSONObject.toJSONString(sn));
        log.info("被调用了...timestamp..{}", JSONObject.toJSONString(timestamp));

        List<User> list = userService.list();
        return R.ok().put("200", "成功!")
                .put("time", DateUtils.getLocalTime())
                .put("date", list);
    }

}
